﻿//-----------------------------------------------------------------------
// <copyright file="TileToolApplication.cs" company="Nathan Miller">
// Copyright (c) Nathan Miller.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
//-----------------------------------------------------------------------
namespace App
{
    using System.Windows.Forms;
    using App.Modules;
    using App.Modules.Action;
    using App.Modules.Camera;
    using App.Modules.ChangeTrack;
    using App.Modules.Clipboard;
    using App.Modules.Command;
    using App.Modules.Component;
    using App.Modules.Editor;
    using App.Modules.EntityFactory;
    using App.Modules.Group;
    using App.Modules.Input;
    using App.Modules.Message;
    using App.Modules.Package;
    using App.Modules.Render;
    using App.Modules.Scene;
    using App.Modules.Settings;
    using App.Modules.Test;
    using App.Modules.Tool;
    using App.Modules.UI;
    using TileTool.Log;
    using TileTool.PerfSpy;

    /// <summary>
    /// TileTool application.
    /// </summary>
    public class TileToolApplication
    {
        #region Constants - Private

        /// <summary>
        /// Log group.
        /// </summary>
        private const string LogGroup = "TileTool";

        #endregion

        #region Fields - Private

        /// <summary>
        /// Application module manager.
        /// </summary>
        private ModuleManager moduleManager = new ModuleManager();

        #endregion

        #region Methods - Public

        /// <summary>
        /// Called to begin the startup process for the application.
        /// </summary>
        public void Startup()
        {
            LogSystem.LogStartup(LogGroup, "Adding application modules...");

            this.AddModules();

            using (new TimedBlock("ModuleStartup"))
            {
                this.moduleManager.Startup();
            }

            this.InstallDefaultTools();
        }

        /// <summary>
        /// Called to create the application's main window.
        /// 
        /// Will be called after Startup() has been called.
        /// </summary>
        /// <returns>Returns the application's main form.</returns>
        public Form CreateMainWindow()
        {
            UIModule interfaceModule = this.moduleManager.GetModule<UIModule>();

            return interfaceModule.CreateMainWindow();
        }

        /// <summary>
        /// Called to give the application a change to work.
        /// </summary>
        public void Work()
        {
            this.moduleManager.Work();
        }

        /// <summary>
        /// Called to shutdown the application.
        /// </summary>
        public void Shutdown()
        {
            this.moduleManager.Shutdown();
        }

        #endregion

        #region Methods - Private

        /// <summary>
        /// After all modules have been started up, install our default set of tools.
        /// </summary>
        private void InstallDefaultTools()
        {
            ToolModule module = this.moduleManager.GetModule<ToolModule>();

            module.InstallDefaultTools();
        }

        /// <summary>
        /// Add all of the application's modules.
        /// 
        /// Add order matters since it will determine startup / shutdown order.
        /// </summary>
        private void AddModules()
        {
            // Base level systems that the editor is built on.
            this.moduleManager.Add(new PackageModule());
            this.moduleManager.Add(new ComponentModule());
            this.moduleManager.Add(new SettingsModule());
            this.moduleManager.Add(new MessageModule());
            this.moduleManager.Add(new CommandModule());
            this.moduleManager.Add(new ActionModule());
            this.moduleManager.Add(new ToolModule());
            this.moduleManager.Add(new SceneModule());
            
            // Change depends on scene. Render and editor depend on change.
            this.moduleManager.Add(new ChangeTrackModule());

            this.moduleManager.Add(new RenderModule());

            // Utility modules.
            this.moduleManager.Add(new InputModule());
            this.moduleManager.Add(new CameraModule());
            this.moduleManager.Add(new EditorModule());
            this.moduleManager.Add(new UIModule());
            this.moduleManager.Add(new ClipboardModule());
            this.moduleManager.Add(new EntityFactoryModule());
            this.moduleManager.Add(new GroupModule());

            // Useless temp module
            this.moduleManager.Add(new TestModule());
        }

        #endregion
    }
}
